/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.drawer;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import com.cv4j.app.ResourceTable;

import java.util.List;

/**
 * @since 2021-05-14
 */
public class DrawerProvider extends BaseItemProvider {
    List<MenuItem> list;

    public DrawerProvider(List<MenuItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MenuItem getItem(int index) {
        return list.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        Holder holder;
        MenuItem item = list.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(
                componentContainer.getContext()).parse(ResourceTable.Layout_layout_item_menu, null, false);
            holder = new Holder();
            holder.text = (Text) cpt.findComponentById(ResourceTable.Id_text);
            holder.image = (Image) cpt.findComponentById(ResourceTable.Id_image);
            holder.root = cpt.findComponentById(ResourceTable.Id_root);

            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;

            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (Holder) cpt.getTag();
        }
        holder.text.setText(item.menuType.getTitle());
        holder.image.setPixelMap(item.rId);
        if (item.isSelect) {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(
                RgbColor.fromArgbInt(componentContainer.getContext().getColor(ResourceTable.Color_menu_select_bg)));
            holder.root.setBackground(element);
        } else {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(0x00000000));
            holder.root.setBackground(element);
        }
        return cpt;
    }

    /**
     * 用于保存列表项中的子组件信息
     */
    public static class Holder {
        Component root;
        Image image;
        Text text;
    }
}
